<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li {
            line-height: 30px;
            margin-bottom: 10px;
            background-color: red;
        }
    </style>
</head>

<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <script>
        let oLis = document.getElementsByTagName("li");

        for (let i = 0; i < oLis.length; i++) {
            // 给当前这个li绑定事件
            oLis[i].onclick = function () {
                console.log(i);
            }
        }

        /*
            {
                let i = 0;

                oLis[i].onclick = function(){
                    console.log(i);
                }
            }


            {
                let i = 1;

                oLis[i].onclick = function(){
                    console.log(i);
                }
            }


            {
                let i = 2;

                oLis[i].onclick = function(){
                    console.log(i);
                }
            }
        
        */
    </script>
</body>

</html>